package expandingproject;

import java.util.Scanner;

/**
 *
 * @author Inderpreet
 */
public class ExpandingProject {

    public static Scanner keyb = new Scanner(System.in);
    public static CoinFlipper coin = new CoinFlipper();
    public static int heads = 0, tails = 0;
    public static MathPractice math = new MathPractice();
    public static int correct = 0, incorrect = 0;
    public static Calculator calc = new Calculator();
    public static StringManipulator manipulator = new StringManipulator();
    public static int scrambleOffset = 10;
    public static GuessingGame guess = new GuessingGame();

    public static void main(String[] args) {
        String userOption = getUserInput();
        while (!userOption.equalsIgnoreCase("Quit")) {
            if (userOption.equalsIgnoreCase("Calculator")) {
                runCalculator();
                userOption = getUserInput();
            } else if (userOption.equalsIgnoreCase("Coin Flipper")) {
                runCoinFlipper();
                userOption = getUserInput();
            } else if (userOption.equalsIgnoreCase("Math Practice")) {
                runMathPractice();
                userOption = getUserInput();
            } else if (userOption.equalsIgnoreCase("String Manipulator")) {
                runStringManipulator();
                userOption = getUserInput();
            } else if (userOption.equalsIgnoreCase("Guessing Game")) {
                runGuessingGame();
                userOption = getUserInput();
            } else {
                userOption = "Quit";
            }
        }
    }

    public static String getUserInput() {
        System.out.println("What would you like to do?");
        System.out.println("1) Calculator");
        System.out.println("2) Coin Flipper");
        System.out.println("3) Math Practice");
        System.out.println("4) String Manipulator");
        System.out.println("5) Guessing Game");
        System.out.println("Q to quit");
        String userOption = keyb.nextLine();
        if (userOption.equalsIgnoreCase("1") || userOption.equalsIgnoreCase("Calculator")) {
            return "Calculator";
        } else if (userOption.equalsIgnoreCase("2") || userOption.equalsIgnoreCase("Coin Flipper")) {
            return "Coin Flipper";
        } else if (userOption.equalsIgnoreCase("3") || userOption.equalsIgnoreCase("Math Practice")) {
            return "Math Practice";
        } else if (userOption.equalsIgnoreCase("4") || userOption.equalsIgnoreCase("String Manipulator")) {
            return "String Manipulator";
        } else if (userOption.equalsIgnoreCase("5") || userOption.equalsIgnoreCase("Guessing Game")) {
            return "Guessing Game";
        } else if (userOption.equalsIgnoreCase("Q") || userOption.equalsIgnoreCase("Quit")) {
            return "Quit";
        }
        return "";
    }

    public static void runCalculator() {
        System.out.println("Welcome to the Calculator");
        String userInput = calc.getUserInput();
        if (userInput.equalsIgnoreCase("Add")) {
            calc.addNumbers();
        } else if (userInput.equalsIgnoreCase("Subtract")) {
            calc.subtractNumbers();
        } else if (userInput.equalsIgnoreCase("Multiply")) {
            calc.multiplyNumbers();
        } else if (userInput.equalsIgnoreCase("Divide")) {
            calc.divideNumbers();
        }
    }

    public static void runCoinFlipper() {
        String playAgain = "";
        while (!playAgain.equalsIgnoreCase("N")) {
            heads = 0;
            tails = 0;
            int flips = coin.getUserInput();
            coin.flipACoin(flips);
            String winner = coin.checkWinner();
            coin.printResults(winner);
            System.out.println("Play again? (Y/N)");
            playAgain = keyb.nextLine();
        }
    }

    public static void runMathPractice() {
        String playAgain = "";
        while (!playAgain.equalsIgnoreCase("N")) {
            correct = 0;
            incorrect = 0;
            try {
                String testType = math.getTestType();
                int questions = math.setNumberOfQuestions();
                long startTime = System.currentTimeMillis();
                math.performTest(questions, testType);
                math.printResults(questions, startTime);
                playAgain = math.testOver();
            } catch (Exception a) {
                System.out.println(a);
                System.out.println("Not valid input!");
                playAgain = math.testOver();
            }
        }
    }

    public static void runStringManipulator() {
        String playAgain = "";
        System.out.println("Welcome to the String Manipulator");
        while (!playAgain.equalsIgnoreCase("N")) {
            String userOption = manipulator.getUserOption();
            String userString = manipulator.getUserString(userOption);
            manipulator.stringManipulator(userString, userOption);
            System.out.println("Play again? (Y/N)");
            playAgain = keyb.nextLine();
        }
    }

    public static void runGuessingGame() {
        String playAgain = "";
        int plays = 0;
        int guesses;
        int userGuess = 0;
        int totalGuesses = 0;
        System.out.println("Welcome to the Guessing Game");
        while (!playAgain.equalsIgnoreCase("N")) {
            plays++;
            guesses = 0;
            int computerNumber = guess.getComputerNumber();
            while (userGuess != computerNumber) {
                userGuess = guess.getUsersGuess();
                guesses++;
                totalGuesses++;
                guess.makeDecisions(userGuess, computerNumber);
            }
            guess.printWinningSequence(guesses);
            System.out.println("Play again? (Y/N)");
            playAgain = keyb.nextLine();
            userGuess = 0;
        }
        guess.printFinalScore(plays, totalGuesses);
    }
}
